package trade.presenter;

import android.support.annotation.NonNull;

import Utils.ActivityUtil;
import Utils.HelloWordModel;
import dao.BaseOutput;
import dao.MerchantTradeInput;
import dao.MerchantTradeOutput;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import trade.view.MerchantTradeView;

/**
 * by BF00233 on 2018/12/21.
 */
public class MerchantTradePresenter {

    private final MerchantTradeView iView;

    public MerchantTradePresenter(MerchantTradeView view) {
        iView = view;
    }

    public void getTradeRecord(String month, String merchantNo) {
        iView.showProgress();
        MerchantTradeInput input = new MerchantTradeInput();
        input.month = month;
        input.merchantNo = merchantNo;

        HelloWordModel.getInstance(iView.getHostActivity())
                .getMerchantTrade(input).enqueue(new Callback<BaseOutput<MerchantTradeOutput>>() {
            @Override
            public void onResponse(@NonNull Call<BaseOutput<MerchantTradeOutput>> call, @NonNull Response<BaseOutput<MerchantTradeOutput>> response) {
               try{
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
                BaseOutput<MerchantTradeOutput> body = response.body();
                if (body == null) {
                    return;
                }
                if ("success".equals(body.msg) && body.data != null) {
                    if (body.data.getItems() != null) {
                        iView.showTradeRecord(body.data.getItems());
                    }
                }
               } catch (Exception e) {
                   e.printStackTrace();
               }
            }

            @Override
            public void onFailure(@NonNull Call<BaseOutput<MerchantTradeOutput>> call, @NonNull Throwable t) {
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
            }
        });

    }

}
